// haoduogeni/addAccount/index.js

var t = getApp();
const { getCodeIpad, carGetQrcode } = require('../common/api/send')
const api = require('../common/utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isCheched: false,
    codeShow: false,
    selectAddressShow: false,
    address: '',//地址
    notes: '',//备注
    uid: '',//pc端uid
    codeId: '',//绑定id
    group_id: "",//分组id
    DeviceID: "",//设备id
    QrCode: "",//二维码
    uuid: "",//检测uuid
    selectEqTypeShow: false,
    eqType: '通道1(默认推荐)',
    login_type: 1,//1ipad 2车载
    isLoading: false,
  },
  selectAdd(){
    this.setData({
      selectAddressShow: true
    })
  },
  selectEqType(){
    this.setData({
      selectEqTypeShow: true
    })
    
  },
  loginSuccess(){
    wx.reLaunch({
      url: '/haoduogeni/mine/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options,'options')
    if(options.obj){
      var obj = JSON.parse(options.obj)
      console.log(obj,'11111111111')
      this.setData({
        uid: obj.uid,
        codeId: obj.id,
        DeviceID: obj.DeviceId,
        address: obj.address,
        notes: obj.notes,
        login_type: obj.login_type,
        eqType: obj.login_type == 1 ? '通道1(默认推荐)' : '通道2'
      })
    }else {
      console.log('22222222222')
      this.setData({
        uid: options.uid,
        codeId: options.id
      })
    }
    
  },
  confirmAddress(e){
    this.setData({
      address: e.detail.join(',')
    })
  },
  confirmEqType(e){
    this.setData({
      eqType: e.detail,
      login_type: e.detail == '通道1(默认推荐)' ? 1 : 2
    })
  },
  changeNotes(e){
    console.log(e,'eeeee')
    this.setData({
      notes: e.detail.value
    })
  },
  changeDeviceID(e){
    console.log(e,'eeeee')
    this.setData({
      DeviceID: e.detail.value
    })
  },
  getCodecIpad(){
    api.post(getCodeIpad,{
      uid: this.data.uid,
      DeviceID: this.data.DeviceID,
      group_id:  this.data.group_id,
      address: this.data.address,
      title: this.data.notes,
    }).then(res => {
      //成功时回调函数
        console.log(res,'getCodeIpad')
        if(res.errno == 0){
          this.setData({
            isLoading: false
          })
          wx.hideLoading();
          this.setData({
            codeShow: true,
            QrCode: res.data.QrBase64,
            uuid: res.data.Uuid
          })
        }else {
          this.setData({
            isLoading: false
          })
          wx.hideLoading();
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      }).catch(err => {
      //失败时回调函数
        console.log(err)
        this.setData({
          isLoading: false
        })
        wx.hideLoading();
      })
  },
  getCodeCar(){
    api.post(carGetQrcode,{
      uid: this.data.uid,
      DeviceID: this.data.DeviceID,
      group_id:  this.data.group_id,
      address: this.data.address,
      title: this.data.notes,
    }).then(res => {
      //成功时回调函数
        console.log(res,'getCodeIpad')
        if(res.errno == 0){
          this.setData({
            codeShow: true,
            QrCode: res.data.QrBase64,
            uuid: res.data.Uuid
          })
          this.setData({
            isLoading: false
          })
          wx.hideLoading();
        }else {
          this.setData({
            isLoading: false
          })
          wx.hideLoading();
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      }).catch(err => {
      //失败时回调函数
        console.log(err)
        this.setData({
          isLoading: false
        })
        wx.hideLoading();
      })
    
  },
  getCodeBtn(){
    if(this.data.isLoading){
      wx.showToast({
        title: '请勿重复点击',
        icon: 'none'
      })
      return 
    }
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      isLoading: true
    })
    if(this.data.login_type == 1){
      this.getCodecIpad()
    }else {
      this.getCodeCar()
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})